export * from "js-base64";
import { fromUint8Array, toUint8Array } from "js-base64";
import { cloneUint8Array, type } from "./utils";
/** 将Uint8Array转化为string */
export function u8a2s(p: Uint8Array) {
  if (type(p).toLowerCase().indexOf("array") >= 0) {
    return fromUint8Array(cloneUint8Array(p));
  }
}
/** 将 string转化为Uint8Array */
export function s2u8a(p: any) {
  if (p instanceof Uint8Array) {
    return p;
  }
  if (type(p).toLowerCase().indexOf("array") >= 0) {
    return cloneUint8Array(p);
  }
  try {
    return toUint8Array(p);
  } catch (e) { }
  return new Uint8Array();
}
export function base64ToFile(base64: string, fileName = 'image.png') {
  // const byteString = window.atob(base64.split(',')[1]);
  // const ab = new ArrayBuffer(byteString.length);
  // const ia = new Uint8Array(ab);
  // for (let i = 0; i < byteString.length; i++) {atob
  //   ia[i] = byteString.charCodeAt(i);
  // }
  const ab = new Uint8Array(s2u8a(base64.split(',')[1]));
  const blob = new Blob([ab], { type: 'image/png' });
  const file = new File([blob], fileName, { type: 'image/png' });
  return file;
}
